package com.keur.money.ui.user.act

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.keur.money.R
import com.keur.money.base.adapter.MullaOnItemChildClickListener
import com.keur.money.base.adapter.MullaRecyclerViewAdapter
import com.keur.money.base.adapter.MullaViewHolderHelper
import com.keur.money.databinding.ActMullaUserAvatarBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.ProAct
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import com.keur.money.utils.helper.KeurCustomDataHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/16 10:47
 * @Annotation：用户头像
 */
class KeurUserAvatarAct: ProAct() {
    lateinit var binding: ActMullaUserAvatarBinding

    //头像列表
    private var avatarAdapter: MullaUserAvatarAdapter? = null
    private var selectPosition = -1
    override fun getMullaRootView(): View {
        binding = ActMullaUserAvatarBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTextTitle(getString(R.string.user_icon))
        setMullaBackBtnRes(R.mipmap.mulla_back_black)
        setMullaRightBtnRes(R.mipmap.mulla_kefu_icon)
        initMullaRecyclerView()

        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(licenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    override fun initMullaListener() {
        //客服
        rootViewBinding.ivFun.setOnClickListener {
            showMullaKefuDialog("User Avatar Page")
            HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_CUSTOMER,"User Avatar Page")
        }
        //确定
        binding.btnSure.setOnClickListener {
            avatarAdapter?.let {
                showMullaLoading(isTOuchOut = false, isCancel = true)
                val selectItem = it.getItem(selectPosition)
                KeurCustomDataHelper.saveMullaCustomUserAvatar(selectItem.optInt("userAvatarId",1))
            }
        }
    }

    override fun initMullaData() {
        val avatarList = arrayListOf<JSONObject>()
        val dataList = KeurCustomDataHelper.userAvatarList
        for (i in 0..<dataList.size){
            val item = JSONObject()
            val userAvatarId = i + 1
            item.put("userAvatarId",userAvatarId)
            if (KeurCustomDataHelper.getMullaCustomUserAvatarId() == userAvatarId){
                item.put("isSelected",true)
                selectPosition = i
            } else {
                item.put("isSelected",false)
            }
            item.put("avatarRes",dataList[i])
            avatarList.add(item)
        }
        avatarAdapter?.data = avatarList
    }

    /**
     * 初始化头像列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initMullaRecyclerView() {
        val layoutManager = GridLayoutManager(this,4)
        binding.recyclerView.layoutManager = layoutManager
        avatarAdapter = MullaUserAvatarAdapter(binding.recyclerView)
        binding.recyclerView.adapter = avatarAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        avatarAdapter?.setOnItemChildClickListener(MullaOnItemChildClickListener { _, childView, position ->
            avatarAdapter?.let {
                val item = it.getItem(position)
                if (item.optBoolean("isSelected", false)) {
                    return@MullaOnItemChildClickListener
                }
                selectPosition = position
                for (i in 0..<it.itemCount) {
                    val model = it.getItem(i)
                    model.put("isSelected", false)
                    if (i == position) {
                        model.put("isSelected", true)
                    }
                }
                it.notifyDataSetChanged()
            }
        })
    }

    class MullaUserAvatarAdapter(recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView,R.layout.item_mulla_user_avatar) {
        override fun setItemChildListener(helper: MullaViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
            val imgAvatar = helper.getImageView(R.id.imgAvatar)
            imgAvatar.setImageResource(model.optInt("avatarRes"))

            if (model.optBoolean("isSelected",false)){
                helper.setVisibility(R.id.imgSelected,View.VISIBLE)
            } else {
                helper.setVisibility(R.id.imgSelected,View.GONE)
            }
        }
    }

    override fun onKeurEvent(event: KeurEventData) {
        when(event.eventType){
            KeurEventData.TYPE.KEUR_CUSTOM_DATA_REFRESHED -> {//自定义数据刷新完成
                hideMullaLoading()
                finish()
            }
            else -> {

            }
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_USER_AVATAR_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_USER_AVATAR_END)
    }
}